﻿using ContractLibrary;
using ContractLibrary.Data;
using RILABomberman.img;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Xna.Framework.Graphics;

namespace RILABomberman
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class ClientMenu : Window
    {
        public static Player _player { get; set; }
        public static IContractServer server = Connection.getServer();
        public static Thread menuWaitThread;


        public ClientMenu()
        {
            InitializeComponent();
            //TODO connection au serveur
            _player = new Player();
            List<Player> players = server.getAllPlayers();
                    
            this.radioButton_teamBlue.IsChecked = _player.equipe == Constants.BLUE_TEAM_ID;
            this.radioButton_teamRed.IsChecked = _player.equipe == Constants.RED_TEAM_ID;

            this.refrshPlayers(players);

            using(ImageCharacter ic = new ImageCharacter()){
                 ImageCharacter.LoadImageCharacter();
                 this.loadImg(ImageCharacter.getCurrent());
            }

        }

        private void button_start_Click(object sender, RoutedEventArgs e)
        {
            _player.equipe = this.radioButton_teamBlue.IsChecked.GetValueOrDefault(false) ? Constants.BLUE_TEAM_ID : Constants.RED_TEAM_ID;
            _player.nom = this.textBox_pseudo.Text;
            _player.characterName = ImageCharacter.getCurrent();
            _player.id = server.Connect(_player);
            this.Hide();
            this.messageWaitPlease();
            //Program.startGame();
        }
        private void messageWaitPlease()
        {
            menuWaitThread = new Thread(() =>
            {
                new ClientMenuWait().Show();
                System.Windows.Threading.Dispatcher.Run();
            });
            menuWaitThread.SetApartmentState(ApartmentState.STA);
            menuWaitThread.Start();
        }

        public void Run()
        {

        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            //Chargement de l'img suivante en fonction de son nom.
            loadImg(ImageCharacter.getNext());
        }

        private void btnBefore_Click(object sender, RoutedEventArgs e)
        {
            //Chargement de l'img précédente en fonction de son nom.
            loadImg(ImageCharacter.getPrevious());
        }


        public void loadImg(String _sImgName)
        {
            Uri uriSource = new Uri(Environment.CurrentDirectory + @"\" + _sImgName);
            
            BitmapImage tempImg = new BitmapImage();
            tempImg.BeginInit();
            tempImg.CacheOption = BitmapCacheOption.OnLoad;
            tempImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            tempImg.UriSource = uriSource;
            tempImg.EndInit();
            this.ImagePerso.Source = tempImg;
        }

        public void refrshPlayers(List<Player> players)
        {
           Dispatcher.FromThread(Program.tMenu).BeginInvoke(DispatcherPriority.Background,new Action(()=>{
               this.dataGrid_TeamBlue.ItemsSource = players.Where(x => (x != null && x.isInTeam(Constants.BLUE_TEAM_ID)));
               this.dataGrid_TeamRed.ItemsSource = players.Where(x => (x != null && x.isInTeam(Constants.RED_TEAM_ID)));
            }));
        }

    }
}
